/* 
 * File:   clock.cpp
 * Author: Elias Capito, Jonathan Neuhaus
 * 
 * Created on 6. mai 2010, 15:14
 */

#include "clock.h"

#include "utils.h"
#include "processing.h"

Clock::Clock(char* image) {
	src = Utils::loadImage(image);
	min = -1;
	hour = -1;
	path = image;
}

//Processus de détection d'une heure sur une horloge
void Clock::detectHour() {
	IplImage ** stackHSV = Processing::getHSV(src);
	
	IplImage* bri = stackHSV[2];
	
	IplImage* briEqual = Processing::equalizeHistGray(bri);
	
	IplImage* briEqualThresh = Processing::thresholdImgGray(briEqual, 225, 255, CV_THRESH_BINARY_INV);
	
	IplImage* briEquThrInvert = Processing::invertImgGray(briEqualThresh);
	
	IplImage* morphClose = Processing::morphCloseImg(briEquThrInvert, 18);
	
	IplImage* morphCloseOpen = Processing::morphOpenImg(morphClose, 18);
	
	IplImage* morphInvert = Processing::invertImgGray(morphCloseOpen);
	
	IplImage* briEqualSub = Processing::subImgGray(briEqual, morphInvert);
	
	float* centre_rayon = Processing::detectCircleGray(briEqualSub, 50, 230);
	
	int x = (int)centre_rayon[0];
	int y = (int)centre_rayon[1];
	int rayon = (int)centre_rayon[2];
	
	IplImage* resizedClock = Processing::crop(briEqualSub, x, y, rayon);
	
	IplImage* resizedAndThreshold = Processing::thresholdImgGray(resizedClock, 70, 255, CV_THRESH_BINARY);
	
	int* tabNeedles = Processing::getTabNeedles(resizedAndThreshold, x, y, rayon);
	
	int* time = Processing::getTime(tabNeedles);
	
	hour = time[0];
	min = time[1];
	drawHour(x,y,rayon);
	
	char buffer[50];
	sprintf(buffer, "%s-1-bri.jpg",path);
	Utils::saveImage(bri,buffer);
	
	sprintf(buffer, "%s-2-briEqual.jpg",path);
	Utils::saveImage(briEqual,buffer);
	
	sprintf(buffer, "%s-3-briEqualThresh.jpg",path);
	Utils::saveImage(briEqualThresh,buffer);
	
	sprintf(buffer, "%s-4-briEquThrInvert.jpg",path);
	Utils::saveImage(briEquThrInvert,buffer);
	
	sprintf(buffer, "%s-5-morphClose.jpg",path);
	Utils::saveImage(morphClose,buffer);
	
	sprintf(buffer, "%s-6-morphCloseOpen.jpg",path);
	Utils::saveImage(morphCloseOpen,buffer);
	
	sprintf(buffer, "%s-7-morphInvert.jpg",path);
	Utils::saveImage(morphInvert,buffer);
	
	sprintf(buffer, "%s-8-briSub.jpg",path);
	Utils::saveImage(briEqualSub,buffer);
	
	sprintf(buffer, "%s-9-ImageIntermediaire.jpg",path);
	Utils::saveImage(resizedClock,buffer);
	
	sprintf(buffer, "%s-10-ImageIntermediaire2.jpg",path);
	Utils::saveImage(resizedAndThreshold,buffer);
}

//Affiche l'heure et les aiguilles sur l'horloge
void Clock::drawHour(int x,int y, int rayon){
	IplImage* clock = Utils::drawCircle(x,y,rayon,src,cvScalar(0,0,255));
	IplImage* center = Utils::drawCross(x,y,clock,cvScalar(0,0,255));
	int xMin = (int) floor(x + (rayon/1.2) * cos(60*min * M_PI / 1800.0 - M_PI / 2.0));
	int yMin = (int) floor(y + (rayon/1.2) * sin(60*min * M_PI / 1800.0 - M_PI / 2.0));
	IplImage* minNeedle = Utils::drawLign(x,y,xMin,yMin,center,cvScalar(0,255,0),"Minutes");
	int xHour = (int) floor(x + (rayon/1.85) * cos(300*(hour+min/60.0) * M_PI / 1800.0 - M_PI / 2.0));
	int yHour = (int) floor(y + (rayon/1.85) * sin(300*(hour+min/60.0) * M_PI / 1800.0 - M_PI / 2.0));
	IplImage* hourNeedle = Utils::drawLign(x,y,xHour,yHour,minNeedle,cvScalar(255,0,0),"Heures");
	IplImage* hourDetect = Utils::drawTextHour(30,50,hourNeedle,cvScalar(0,0,255),hour,min);
	Utils::showImage(hourDetect,path);
	Utils::saveImage(hourDetect, path);
}
Clock::~Clock() {
	delete src;
	delete path;
}
